<?php
/**
 * 后台文件基类
 * */
namespace app\admin\controller;
use app\common\controller\ControllerBase;
use think\Cache;
use think\Db;
use think\Request;
use think\Session;


class AdminBase extends  ControllerBase
{
    protected $IsAuth = true;      // 权限检测  true:开启；alse:关闭
    protected $AuthWhite = true;  // 权限验证白名单

    public function _initialize()
    {
        parent::_initialize();
        
        // 检测是否登录
        $login = Model('user')->isLogin();
        if($login !=1){
            $this->redirect('admin/login/index');
         }

        // 权限检测
        $auth = $this->checkAuth();
        if($this->IsAuth ==true){

            if($auth['status'] !=1){
                //print_r($auth);
                $this->error('权限不够',url('index/info'),3);
            }
        }else{
            print_r($auth);
        }

        // 实例化后基础数据
        $site = Model('site')->info();
        $this->assign('site',$site);
    }

    /**
     * 检测权限
     * */
    public function checkAuth(){
        $request = request::instance();
        $model = $request->module();
        $controller  = $request->controller();
        $action = $request->action();
        $user = Session::get('user');

        // 拼接字符
        $auth['name'] = $model.'/'.$controller.'/'.$action;

        // 查询节点对于ID
        $auth['auth_id']  = Model('auth')->roleReturnID($auth['name']);
        $auth['user_auth'] = $user['auth_list'];

        if($auth['auth_id']){
           if(strstr($auth['user_auth'],''.$auth['auth_id'].'') == true){
                $auth['status'] =1;
            }else{
                $auth['status'] = 0;
            }
        }else{
            // 如果查询到的节点id为空 ，则无权限
            $auth['status'] = 0;
        }


        // 验证白名单
        if($this->AuthWhite == true){
            $arr = ['1'];
            if(in_array($user['id'],$arr)){
                $auth['status'] = 1;
                $auth['user_auth'] = 'all';
            }
        }

        return $auth;

    }

    /**
     * 执行成功或者失败
     * @param  int $v 1或者0
     * */
    public function is_success($v){
        if($v==1){
            $this->success(lang('success'));
        }else{
            $this->error(lang('error'));
        }
    }
    /**
     * 审核与未审核
     * */
    public function isCheck($id=null){
        $arr = lang('check');
        $opt = langOpt($arr,$id);
        return $opt;
    }



}